<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单报表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="8">
          <div class="grid-content bg-purple">
            <el-input
              placeholder="请输入内容"
              v-model="queryinfo.query"
              clearable
              class="input-with-select"
              @change="getOrderList"
            >
              <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
            </el-input>
          </div>
        </el-col>
        <el-col :span="16">
          <div class="grid-content bg-purple-light"></div>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="tableData" border stripe style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <div>
              <el-tag v-if="scope.row.pay_status ==='1'">已付款</el-tag>
              <el-tag type="danger" v-else>未付款</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">{{scope.row.create_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <div>
              <!-- 修改订单地址 -->
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditAddress"></el-button>
              <!-- 物流信息 -->
              <el-button
                type="success"
                icon="el-icon-location-outline"
                size="mini"
                @click="logistics"
              ></el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 修改订单的对话框 -->
      <el-dialog
        title="修改地址"
        :visible.sync="dialogVisible"
        width="50%"
        @close="addressDialogClosed"
      >
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="省市区/县" prop="City">
            <el-cascader v-model="ruleForm.City" :options="citydata"></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address">
            <el-input v-model="ruleForm.address"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 物流信息 -->
      <el-dialog title="物流进度" :visible.sync="logisticsdialogVisible" width="50%">
        <el-timeline>
          <el-timeline-item
            v-for="(activity, index) in Logistics"
            :key="index"
            :timestamp="activity.time"
          >{{activity.context}}</el-timeline-item>
        </el-timeline>
        <span slot="footer" class="dialog-footer">
          <el-button @click="logisticsdialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="logisticsdialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[5, 20, 30, 40]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import citydata from "@/components/citydata.js";
export default {
  data() {
    return {
      tableData: [],
      queryinfo: {
        query: "",
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      //控制修改订单的对话框
      dialogVisible: false,
      //修改收货地址的表单
      ruleForm: {
        City: [],
        address: ""
      },
      rules: {
        City: [
          { required: true, message: "请选择收货地址", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        address: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ]
      },
      //   数据源
      citydata: citydata,
      // 物流弹框
      logisticsdialogVisible: false,
      // 存放物流数组
      Logistics: [],
      
    };
  },
  created() {
    this.getOrderList();
  },

  methods: {
    //请求数据
    async getOrderList() {
      const { data: res } = await this.$http.get("orders", {
        params: this.queryinfo
      });
      console.log(res);
      this.tableData = res.data.goods;
      this.total = res.data.total;
    },
    // 分页
    handleSizeChange(val) {
      this.queryinfo.pagesize = val;
      this.getOrderList();
    },
    handleCurrentChange(val) {
      this.queryinfo.pagenum = val;
      this.getOrderList();
    },
    //修改地址
    showEditAddress() {
      this.dialogVisible = true;
    },
    // 关闭
    addressDialogClosed() {
      this.ruleForm.City = [];
    },
    // 物流信息
    async logistics() {
      const { data: res } = await this.$http.get("/kuaidi/1106975712662");
      this.Logistics = res.data;
      console.log(res.data);
      if (res.meta.status !== 200) {
        return this.$message.error("获取物流进度失败!");
      }
      this.logisticsdialogVisible = true;
    }
  }
};
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 20px;
}
.el-table {
  margin-top: 10px;
}
.el-cascader {
  width: 100%;
}
</style>